package org.arain.baseii.system.department;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.arain.baseii.aspect.annotation.SystemExceptionLog;
import org.arain.baseii.aspect.annotation.SystemServiceLog;
import org.arain.baseii.common.CommonServiceImpl;
import org.arain.baseii.mapper.SysDepartmentMapper;
import org.arain.baseii.mapper.SysGroupMapper;
import org.arain.baseii.pojo.SysDepartment;
import org.arain.baseii.pojo.SysDepartmentExample;
import org.arain.baseii.pojo.SysGroup;
import org.arain.baseii.pojo.SysGroupExample;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.arain.baseii.utils.DictPojo;
import org.arain.baseii.utils.DictUtil;
import org.arain.baseii.utils.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * 
* <p>Title:DepartmentServiceImpl </p>
* @author Arain
* @date2017年10月23日
 */
@Service
@Transactional
public class DepartmentServiceImpl extends CommonServiceImpl<DepartmentDto> implements DepartmentService{
	@Autowired
	private SysGroupMapper sysGroupMapper;
	@Autowired
	private SysDepartmentMapper sysDepartmentMapper;
	
	@SuppressWarnings("unchecked")
	@Override
	@SystemExceptionLog(description="保存")
	@SystemServiceLog(description="保存")
	public ArainResult save_01(DepartmentDto departmentDto, HttpServletRequest request) {
		net.sf.json.JSONArray resourceJsons=net.sf.json.JSONArray.fromObject(departmentDto.getResourcesJson());
		if(StringUtils.isNotBlank(departmentDto.getId())) {
			SysGroup group = new SysGroup();
			AppUtils.copyProperties(group, departmentDto);
			sysGroupMapper.updateByPrimaryKey(group);
		} else {
			SysGroup group = new SysGroup();
			AppUtils.copyProperties(group, departmentDto);
			sysGroupMapper.insert(group);
		}
		SysDepartmentExample example = new SysDepartmentExample();
		sysDepartmentMapper.deleteByExample(example);
		
		List<SysDepartment> SysDepartmentDtos=(List<SysDepartment>) net.sf.json.JSONArray.toCollection(resourceJsons, SysDepartment.class);
		for (SysDepartment sysDepartment : SysDepartmentDtos) {
			sysDepartment.setId(Long.valueOf(IDUtil.genId()));
			sysDepartment.setOrganizationId(Long.valueOf(departmentDto.getOrgid()));
			sysDepartmentMapper.insert(sysDepartment);
		}
		return ArainResult.ok();
	}

	@Override
	public ArainResult load_01(DepartmentDto departmentDto, HttpServletRequest request) {
		SysGroupExample example = new SysGroupExample();
		example.createCriteria().andOrgidEqualTo(AppUtils.getOrgId());
		List<SysGroup> list = sysGroupMapper.selectByExample(example);
		if(list!=null && list.size()>0) {
			list.get(0).setOrgid(AppUtils.getOrgId());
			return ArainResult.ok(list.get(0));
		}
		SysGroup group = new SysGroup();
		group.setOrgid(AppUtils.getOrgId());
		return ArainResult.ok(group);
	}

	@Override
	public String load_orgid(Long orgid) {
		if(orgid==null) {
			return "";
		}
		com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
		SysDepartmentExample example = new SysDepartmentExample();
		example.createCriteria().andOrganizationIdEqualTo(orgid);
		List<SysDepartment> list = sysDepartmentMapper.selectByExample(example);
		for (SysDepartment sysDepartment : list) {
			JSONObject object = new JSONObject();
			object.put("name", sysDepartment.getName());
			object.put("number", sysDepartment.getNumber());
			if (sysDepartment.getEnabled() == 1) {
				object.put("enabled", 1);
			} else {
				object.put("enabled", 0);
			}
			array.add(object);
		}
		return array.toString();
	}

	@Override
	public String load_type() {
		List<DictPojo> list = DictUtil.load_dict_list("groupType");
		JSONArray array = new JSONArray();
		for (DictPojo dictPojo : list) {
			JSONObject object = new JSONObject();
			object.put("id", dictPojo.getValue());
			object.put("text", dictPojo.getDispaly());
			array.add(object);
		}
		return array.toString();
	}

	@Override
	public String load_enabled() {
		JSONArray array = new JSONArray();
		SysDepartmentExample example = new SysDepartmentExample();
		example.createCriteria().andOrganizationIdEqualTo(AppUtils.getOrgId()).andEnabledEqualTo((short) 1);
		List<SysDepartment> list = sysDepartmentMapper.selectByExample(example);
			for (SysDepartment sysDepartment : list) {
				JSONObject object = new JSONObject();
				object.put("id", sysDepartment.getId());
				object.put("text", sysDepartment.getName());
				array.add(object);
		}
		return array.toString();
	}

}
